﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using P2P.Stun.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using P2P.Network.ExtensionMethods;

namespace P2P.Stun.Handler
{
    public class NatCheckHandler
    {
        private ApiProtocol apiProtocol;

        public NatCheckHandler(ApiProtocol apiProtocol)
        {
            this.apiProtocol = apiProtocol;
        }

        public void Step1() 
        {
            string AccountId = (string)apiProtocol.Params["AccountId"];

            JObject data = new JObject();
            data.Add("Name", "NatCheck");
            data.Add("Method", "Step1");
            data.Add("Version", "1.0");
            JObject param = new JObject();
            param.Add("AccountId", AccountId);
            param.Add("LocalIP", apiProtocol.RemoteEndPoint.Address.ToString());
            param.Add("LocalPort", apiProtocol.RemoteEndPoint.Port);
            data.Add("Params", param);
            string ack = JsonConvert.SerializeObject(R<JObject>.Ok(data));

            apiProtocol.Socket.SendTo(ack,SharedModel.Instance.Hosts.UDPMainServerFirstIP, 
                SharedModel.Instance.Hosts.UDPMainServerFirstPort.Value);
        }
    }
}
